<template>
  <a-modal
    title="新增基本信息校验"
    width="50%"
    :centered="true"
    wrapClassName="common_modal"
    v-model:open="visible"
    :confirm-loading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-form
      ref="addFormRef"
      :model="formInfo"
      :rules="rules"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
    >
      <a-row>
        <a-col :span="12">
          <a-form-item label="警告级别" name="warnLevel">
            <a-select v-model:value="formInfo.warnLevel" popupClassName="common_select_dropdown">
              <a-select-option v-for="item in warnLevelList" :value="item.value" :key="item.value">
                {{ item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="性别" name="sex">
            <a-select v-model:value="formInfo.sex" popupClassName="common_select_dropdown">
              <a-select-option value="男"> 男 </a-select-option>
              <a-select-option value="女"> 女 </a-select-option>
              <a-select-option value="其他"> 其他 </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item
            label="最小年龄"
            name="minAge"
            :label-col="{ span: 9 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-input-number v-model:value="formInfo.minAge" :step="0.1" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item
            label="最大年龄"
            name="maxAge"
            :label-col="{ span: 9 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-input-number v-model:value="formInfo.maxAge" :step="0.1" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item
            label="年龄单位"
            name="ageUnit"
            :label-col="{ span: 9 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-select v-model:value="formInfo.ageUnit" popupClassName="common_select_dropdown">
              <a-select-option value="岁"> 岁 </a-select-option>
              <a-select-option value="月"> 月 </a-select-option>
              <a-select-option value="周"> 周 </a-select-option>
              <a-select-option value="天"> 天 </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item
            label="排除字符"
            name="excludeWord"
            :label-col="{ span: 3 }"
            :wrapper-col="{ span: 21 }"
          >
            <a-input v-model:value="formInfo.excludeWord" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="规则依据" name="remark">
            <a-textarea v-model:value="formInfo.remark" :rows="4" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="默认提示" name="warnMsg">
            <a-textarea v-model:value="formInfo.warnMsg" :rows="4" />
          </a-form-item>
        </a-col>

        <a-col :span="12">
          <a-form-item label="扣分数" name="score">
            <a-input-number v-model:value="formInfo.score" :step="0.1" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { validPatientSave } from '@/axios/api/valid_api'
import { addProps } from './props'
import { message } from 'ant-design-vue'
import type { ListType } from '@/axios/api/type.ts'
const emit = defineEmits(['getPage'])
const [messageApi] = message.useMessage()
const addFormRef = ref()
const props = defineProps(addProps)
const warnLevelList = ref<ListType[]>([])
const visible = ref(false)
const confirmLoading = ref(false)
const formInfo = ref({
  sex: '',
  excludeWord: '',
  remark: '',
  warnLevel: '',
  ageUnit: '',
  minAge: null,
  maxAge: null,
  warnMsg: '',
  score: 0
})
const rules = {
  warnLevel: [{ required: true, message: '请选择警告级别', trigger: ['change', 'blur'] }],
  sex: [{ required: true, message: '请选择性别', trigger: ['change', 'blur'] }],
  excludeWord: [{ required: true, message: '请输入排除字符', trigger: ['blur'] }],
  remark: [{ required: true, message: '请输入规则依据', trigger: ['blur'] }],
  minAge: [
    {
      validator: checkAge,
      trigger: ['change', 'blur']
    }
  ],
  ageUnit: [
    {
      validator: checkAgeUnit,
      trigger: ['change', 'blur']
    }
  ],
  maxAge: [
    {
      validator: checkAge,
      trigger: ['change', 'blur']
    }
  ]
}

function checkAge() {
  if (typeof formInfo.value.minAge === 'number' && typeof formInfo.value.maxAge === 'number') {
    if (formInfo.value.minAge < formInfo.value.maxAge) {
      return Promise.resolve()
    } else {
      return Promise.reject('请输入正确的年龄范围')
    }
  } else {
    return Promise.resolve()
  }
}
function checkAgeUnit() {
  if (
    (typeof formInfo.value.minAge === 'number' || typeof formInfo.value.maxAge === 'number') &&
    !formInfo.value.ageUnit
  ) {
    return Promise.reject('请选择年龄单位')
  } else {
    return Promise.resolve()
  }
}
function showModal() {
  visible.value = true
  warnLevelList.value = props.warnLevelList
}
function handleOk() {
  addFormRef.value
    .validate()
    .then(() => {
      confirmLoading.value = true
      validPatientSave(formInfo.value)
        .then((res) => {
          if (res.code === 200) {
            messageApi.success(res.msg)
            emit('getPage')
            handleCancel()
          }
          confirmLoading.value = false
        })
        .catch(() => {
          confirmLoading.value = false
        })
    })
    .catch((err: any) => {
      console.log('%c [ err ]-197', 'font-size:13px; background:pink; color:#bf2c9f;', err)
    })
}
function handleCancel() {
  visible.value = false
  confirmLoading.value = false
  formInfo.value = {
    sex: '',
    excludeWord: '',
    remark: '',
    warnLevel: '',
    ageUnit: '',
    minAge: null,
    maxAge: null,
    warnMsg: '',
    score: 0
  }
}
defineExpose({
  showModal
})
</script>
